package com.jack.thinking.thread.core._6;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

public class SingleModelStaticClassSerial {
	public static void main(String[] args) throws IOException, Exception {
		MyObjectSerial myObject = MyObjectSerial.getInstance();
		FileOutputStream fos = new FileOutputStream(new File("myObjectFile.txt"));
		ObjectOutputStream oosRef = new ObjectOutputStream(fos);
		oosRef.writeObject(myObject);
		oosRef.close();
		fos.close();
		System.out.println(myObject.hashCode());
		
		FileInputStream fis = new FileInputStream(new File("myObjectFile.txt"));
		ObjectInputStream oisRef = new ObjectInputStream(fis);
		MyObjectSerial readObject = (MyObjectSerial) oisRef.readObject();
		oisRef.close();
		fis.close();
		System.out.println(readObject.hashCode());
	}
}

class MyObjectSerial implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static class SingleClass{
		public static MyObjectSerial mo = new MyObjectSerial();
	}
	
	public static MyObjectSerial getInstance() {
		return SingleClass.mo;
	}
	
	public Object readResolve() throws ObjectStreamException{
		System.out.println("call readResolve method");
		return SingleClass.mo;
	}
}

class MyThreadSerial extends Thread{
	@Override
	public void run() {
		System.out.println(MyObject.getInstance().hashCode());
	}
}